package sigcBeans;

import com.franz.agbase.AllegroGraph;
import com.franz.agbase.AllegroGraphException;
import com.franz.agbase.ValueSetIterator;

import java.util.*;

import agraph.*;

public class userBean{


	public userBean() {}


	UserHandling uh = new UserHandling();
	AllegroGraph ag = uh.openRDFStore(uh.openConnection());

	String txtLogin;
	String txtPasswd;

	public boolean fazLogin(String txtLogin,String txtPasswd) throws AllegroGraphException{
		if(uh.userLogin(ag, txtLogin, txtPasswd))
			return true;
		else
			return false;
	}

	public Vector<String> getCategorias() throws AllegroGraphException{
		Vector<String> vec = new Vector<String>();	

		vec = uh.getCategories(ag);
		return vec;
	}

	public Hashtable<String,String> getOrderedCategorias() throws AllegroGraphException{
		Hashtable<String,String> hash = new Hashtable<String,String>();	

		hash = uh.getOrderedCategories(ag);
		return hash;
	}

	public boolean registaUtilizador(String login,String passwd,String primNome,String ultNome,String eMail) throws AllegroGraphException{
		return uh.userRegister(ag, eMail,login, passwd,primNome,ultNome) ;
	}

	public boolean subscreveUtil(String login,String categoria) throws AllegroGraphException{
		return uh.userSubscribe(ag, login, categoria);
	}

	public boolean desSubscreveUtil(String login,String categoria) throws AllegroGraphException{
		return uh.userUnsubscribe(ag, login, categoria);
	}

	public Vector<String> getSubscricoes(String login) throws AllegroGraphException{
		return uh.getSubscricoes(login,ag);
	}

	public Vector<Hashtable> getFeedFromCategory(String category) throws AllegroGraphException{
		return uh.getFeedFromCategory(category,ag);
	}

	public Vector<Hashtable> getUnreadFeedFromCategory(String category, String login) throws AllegroGraphException{
		return uh.getUnreadFeedFromCategory(category, login,ag);//getFeedFromCategory(category);
	}

	/**
	 * 
	 * @param login
	 * @return
	 * @throws AllegroGraphException
	 */
	public int getNumberOfNewFeeds(String login) throws AllegroGraphException{
		int numberOfNewFeeds = 0;
		Vector<String> categorias = new Vector<String>();
		Vector<Hashtable> vec = new Vector<Hashtable>();

		categorias = getSubscricoes(login);		
		for(String s : categorias){
			s = s.substring(45);
			s = s.substring(0,(s.length()-2));
			vec = getUnreadFeedFromCategory(s,login);
			numberOfNewFeeds += vec.size();
			System.out.println("getNumberOfNewFeeds: "+numberOfNewFeeds+" categoria: "+s+" size:"+vec.size());
		}

		return numberOfNewFeeds;
	}

	public void setFeedAsRead(String feedID,String login) throws AllegroGraphException{
		uh.setFeedAsRead(feedID, login,ag);
	}

	public void setFeedsAsNonRead(String login) throws AllegroGraphException{
		uh.setFeedsAsNonRead(login,ag);
	}

	public String getTxtLogin() {
		return txtLogin;
	}
	public void setTxtLogin(String txtLogin) {
		this.txtLogin = txtLogin;
	}
	public String getTxtPasswd() {
		return txtPasswd;
	}
	public void setTxtPasswd(String txtPasswd) {
		this.txtPasswd = txtPasswd;
	}

	public ValueSetIterator getAllFeeds() throws AllegroGraphException{
		return uh.returnAllFeeds(ag);
	}

	public String getCategoryByFeed(String feed) throws AllegroGraphException{
		return uh.getCategoriesByFeed(ag, feed);
	}

	public int getMaxOcurrCategories(Hashtable<String,Integer> hash){
		int maxOcurr = 0, aux = 0;

		Enumeration e = hash.elements();

		//iterate through Hashtable values Enumeration
		while(e.hasMoreElements())
		{ 
			aux = (Integer) e.nextElement();
			if(maxOcurr < aux)
				maxOcurr = aux;
		}

		return maxOcurr;
	}


	public ValueSetIterator getSearchResults(String search)throws AllegroGraphException{
		return uh.getSearchResults(ag, search);

	}
	
	public int getFontSize(int maxOcurr,int ocurr){
	double fontSize = 0;
	//System.out.println("maxOcuur:"+maxOcurr+" cocuurr"+ocurr+"ghjk"+(( ( ocurr/maxOcurr )*(36) )+10));
	fontSize =  (double)ocurr/maxOcurr*36+10; 
	//System.out.println("fontsize "+fontSize);
		return (int)fontSize;//fontSize;
	}
	
	public Hashtable<String, Integer> getCategoryOcurrences() throws AllegroGraphException{

		//Collections.sort( (uh.getCategoryOcurrences(ag)).values());
		return uh.getCategoryOcurrences(ag);
	}
	
	public ValueSetIterator getFeedsByCategory (String category) throws AllegroGraphException{
		return uh.getFeedsByCategory(ag, category);
		
		
	}
	
	public ValueSetIterator getAdvice(String login) throws AllegroGraphException{
		return uh.getAdvice(ag, login);
	}
	
}